package com.hq.fxh.No6UerSpace.Fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.hq.fxh.CustomerView.DividerGridItemDecoration;
import com.hq.fxh.HttpApi.UserServiceImpl;
import com.hq.fxh.Modle.Space_UserVideo_Modle;
import com.hq.fxh.Modle.UserVideoModle;
import com.hq.fxh.No6UerSpace.Adapter.Radapter_UserSpace_Activity;
import com.hq.fxh.No6UerSpace.Adapter.Radapter_UserSpace_AnLi;
import com.hq.fxh.No6UerSpace.Adapter.Radapter_UserSpace_CanPin;
import com.hq.fxh.No6UerSpace.Adapter.Radapter_UserSpace_Video;
import com.hq.fxh.No6UerSpace.Adapter.Radapter_UserSpace_ZhaoPin;
import com.hq.fxh.R;
import com.hq.fxh.Utils.DensityUtils;
import com.hq.fxh.Utils.LogUtils;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * /**
 *
 * @author 薛
 * @ClassName
 * @Description :
 * @Created 2017/9/19
 * @Email xlwenhao1858@sina.com
 */


public class Fragment_UserSpace_Video extends Fragment {
    protected View rootView;
    protected RecyclerView recycleUserSpace;
    protected TextView noData;
    private Radapter_UserSpace_Video videoAdapter;
    Radapter_UserSpace_CanPin canPinAdapter;
    Radapter_UserSpace_AnLi anliAdapter;
    Radapter_UserSpace_ZhaoPin zaoPinAdapter;
    List<UserVideoModle> vdioeData;
    int uid, page = 1;

    public void setUid(int uid) {
        this.uid = uid;

        getUSerVideoList(page, 10, uid);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_userspace_plate, null);
        initView(rootView);
        initData();
        return rootView;
    }

    private void initData() {

        int type = getArguments().getInt("type");
        switch (type) {
            case 0:
                setLinearManager(0);
                break;
            case 1:
                setLinearManager(0);
                break;
            case 2:
                setLinearManager(1);
                break;
            case 3:
                setLinearManager(1);
                break;
            case 4:
                setLinearManager(0);
                break;
            case 5:
                setLinearManager(0);
                break;
        }
        setAdapter(type);

    }

    public void setAdapter(int type) {

        switch (type) {
            case 0:
                setLinearManager(0);
                break;
            case 1:
                videoAdapter = new Radapter_UserSpace_Video();

                recycleUserSpace.setAdapter(videoAdapter);
                break;
            case 2:
                canPinAdapter = new Radapter_UserSpace_CanPin();
                recycleUserSpace.setAdapter(canPinAdapter);
                recycleUserSpace.addItemDecoration(new DividerGridItemDecoration(DensityUtils.dip2px(getContext(), 10), DensityUtils.dip2px(getContext(), 10)));

                break;
            case 3:
                anliAdapter = new Radapter_UserSpace_AnLi();
                recycleUserSpace.setAdapter(anliAdapter);
                recycleUserSpace.addItemDecoration(new DividerGridItemDecoration(DensityUtils.dip2px(getContext(), 10), DensityUtils.dip2px(getContext(), 10)));
                break;
            case 4:
                zaoPinAdapter = new Radapter_UserSpace_ZhaoPin();
                recycleUserSpace.setAdapter(zaoPinAdapter);
                break;
            case 5:
                Radapter_UserSpace_Activity activityAdapter = new Radapter_UserSpace_Activity();
                recycleUserSpace.setAdapter(activityAdapter);
                break;
        }
    }

    public void setLinearManager(int type) {
        if (type == 0) {
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
            recycleUserSpace.setLayoutManager(linearLayoutManager);
            linearLayoutManager.setOrientation(OrientationHelper.VERTICAL);
        } else {
            LinearLayoutManager linearLayoutManager = new GridLayoutManager(getActivity(), 2);
            recycleUserSpace.setLayoutManager(linearLayoutManager);
            linearLayoutManager.setOrientation(OrientationHelper.VERTICAL);
        }
    }

    public static Fragment_UserSpace_Video newInstance(int style) {

        Bundle args = new Bundle();
        args.putInt("type", style);
        Fragment_UserSpace_Video fragment = new Fragment_UserSpace_Video();

        fragment.setArguments(args);
        return fragment;
    }

    private void initView(View rootView) {
        recycleUserSpace = (RecyclerView) rootView.findViewById(R.id.recycle_UserSpace);
        noData = (TextView) rootView.findViewById(R.id.noData);
    }


    public void getUSerVideoList(final int page, int pageSize, int uid) {
        UserServiceImpl.GetUSerVideoList(page, pageSize, uid, new Callback<Space_UserVideo_Modle>() {
            @Override
            public void onResponse(Call<Space_UserVideo_Modle> call, Response<Space_UserVideo_Modle> response) {
                Space_UserVideo_Modle body = response.body();
                if (body == null) {
                    LogUtils.startToash(getContext(), " 获取视频列表出错");
                } else {
                    List<UserVideoModle> data = body.getData();
                    if (data == null) {
                        if (page == 1) {
                            noData.setVisibility(View.VISIBLE);
                        } else {
                            noData.setVisibility(View.GONE);
                        }

                    }

                    if (vdioeData == null) {
                        vdioeData = new ArrayList<UserVideoModle>();
                    }
                    vdioeData.addAll(data);
                    videoAdapter.setVdioeData(vdioeData);
                    videoAdapter.notifyDataSetChanged();


                }

            }

            @Override
            public void onFailure(Call<Space_UserVideo_Modle> call, Throwable t) {

            }
        });
    }
}
